package com.cxyd.modules.wms.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cxyd.common.page.PageData;
import com.cxyd.common.service.impl.CrudServiceImpl;
import com.cxyd.common.utils.ConvertUtils;
import com.cxyd.modules.sys.dto.SysUserDTO;
import com.cxyd.modules.sys.service.SysUserService;
import com.cxyd.modules.wms.dao.LogCheckDao;
import com.cxyd.modules.wms.dao.LogWorkDao;
import com.cxyd.modules.wms.dto.LogCheckDTO;
import com.cxyd.modules.wms.dto.LogWorkDTO;
import com.cxyd.modules.wms.entity.LogCheckEntity;
import com.cxyd.modules.wms.entity.LogWorkEntity;
import com.cxyd.modules.wms.entity.OrderReelEntity;
import com.cxyd.modules.wms.service.LogCheckService;
import com.cxyd.modules.wms.service.LogWorkService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 盘点调整库存
 *
 * @author lp
 * @since 1.0.0 2024-9-10
 */
@Service
public class LogCheckServiceImpl extends CrudServiceImpl<LogCheckDao, LogCheckEntity, LogCheckDTO> implements LogCheckService {
    private final SysUserService userService;

    public LogCheckServiceImpl(SysUserService userService) {
        this.userService = userService;
    }

    @Override
    public PageData<LogCheckDTO> page(Map<String, Object> params) {
        //1.查询日志信息
        final IPage<LogCheckEntity> page = baseDao.selectPage(
                getPage(params, "id", false),
                getWrapper(params)
        );

        //2.获取用户信息
        final List<SysUserDTO> userList = userService.getList();
        //3.将creator与updater的username通过id进行绑定
        page.getRecords().forEach(entity -> {
            if (entity.getCreator() != null) {
                entity.setCreatorName(userService.getUsernameById(entity.getCreator(), userList));
            }
            if (entity.getUpdater() != null) {
                entity.setUpdaterName(userService.getUsernameById(entity.getUpdater(), userList));
            }
        });
        return getPageData(page, LogCheckDTO.class);
    }

    @Override
    public QueryWrapper<LogCheckEntity> getWrapper(Map<String, Object> params)
    {
        final String spuNo = (String) params.get("spuNo");
        final String reelId = (String) params.get("reelId");

        QueryWrapper<LogCheckEntity> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(spuNo), "spu_no", spuNo);
        wrapper.like(StringUtils.isNotBlank(reelId), "reel_id", reelId);

        return wrapper;
    }
}